package com.advaita.gui;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.advaita.license.Customer;
import com.advaita.license.License;
import com.advaita.util.GetMacAddress;
import com.advaita.util.TextPrompt;

public class GetLicense extends JFrame implements KeyListener {
	JTextField licenseField;

	// ------------------------------------
	// Code to check license key
	// ------------------------------------

	public Boolean checkLicense(String licenseKey, Customer customer) {
		// Get the current Date.  Note setting the clock back
		// will make this ineffective
		Date systemDate = new Date();
		
		// Send a licenseKey to the super secret decoder ring
		// and get back customer information
		
	
		//customer = License.decodeSuperSecretCode(licenseKey);
		// Show MAC Address
		System.out.println("MAC Address in checkLicense : "
				+ customer.licenseInfo.getMacAddress());
		
		//-----------------------------------------
		// Check Expiration Date
		//-----------------------------------------
		
		DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
		System.out.println("Expiration Date : "
				+ dateFormat.format(customer.licenseInfo.getExpDate()));
		
		if(systemDate.compareTo(customer.licenseInfo.getExpDate())>0){
    		System.out.println("Expired");
		}
		else
			System.out.println("Not Expired");
	
		//-----------------------------------------
		// Get the computer's Mac Address
		//-----------------------------------------
		GetMacAddress systemMacAddress = new GetMacAddress();
		System.out.println("My system mac address: "
				+ systemMacAddress.myMacAddress);
		
		System.out.println("License Key macAddress : "
				+ customer.licenseInfo.getMacAddress());
		
		// Check to see if the stored macAddress is part of the system macAddress
		System.out.println (" mac addres match :" +systemMacAddress.myMacAddress.toLowerCase().
		contains(customer.licenseInfo.getMacAddress().toLowerCase()));
		return true;

	}

	public GetLicense() {

		super("Pathway Guide Activation");

		String property = "swing.aatext";
		if (null == System.getProperty(property))
			System.setProperty(property, "true");

		String[] data = {
				"  To get an activation license please call/email Advaita",
				"  sales@advaitacorporation.com", "  +1 (734) 922 - 0110" };

		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.PAGE_END;
		// natural height, maximum width
		gbc.fill = GridBagConstraints.HORIZONTAL;

		gbc.ipadx = 40;
		gbc.ipady = 40;

		String path = "images/advaita.png";
		File file = new File(path);
		BufferedImage image;
		try {
			image = ImageIO.read(file);
			JLabel label = new JLabel(new ImageIcon(image));

			panel.add(label, gbc);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		gbc.gridx = 0;
		gbc.gridy = 10;

		String path2 = "images/lockKeySmall.png";
		File file2 = new File(path2);
		BufferedImage image2;
		try {
			image2 = ImageIO.read(file2);
			JLabel label = new JLabel(new ImageIcon(image2));

			// PJM use the screen size for the position
			// label.setBounds(2, 1, 500, 280);

			panel.add(label, gbc);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		gbc.ipadx = 3;
		gbc.ipady = 3;
		gbc.gridx = 0;
		for (int i = 1; i < data.length + 1; i++) {
			JLabel label = new JLabel(data[i - 1]);
			label.setFont(new Font("serif", Font.PLAIN, 16));
			gbc.gridy = i;
			panel.add(label, gbc);
		}

		getContentPane().add(panel);

		// OK, I don't know if you will like how this works.
		// The text disappears in the box when a key is entered.
		licenseField = new JTextField(41);
		TextPrompt tp = new TextPrompt("Enter activation code here",
				licenseField);
		licenseField.setToolTipText("Enter activation code here");
		licenseField.setFont(new Font("serif", Font.PLAIN, 16));

		gbc.gridy = 6;
		gbc.gridx = 0;
		gbc.ipadx = 10;
		gbc.ipady = 10;

		licenseField.addKeyListener(this);

		panel.add(licenseField, gbc);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);

	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_ENTER) {
			String licenseKey = licenseField.getText();

			Boolean myReturn;
			
			Customer customer = new Customer();
			customer = License.decodeSuperSecretCode(licenseKey);
			
			myReturn = checkLicense(licenseKey, customer);
			// 53217115116157196102106052315
			// MAC Address : 0010B5C4666A
			// Expiration Date : 05/23/15
			System.out.println("MAC Address in keypresesed : "
					+ customer.licenseInfo.getMacAddress());
			
		//	if (licenseKey.compareTo("patty") == 0) {
				GoodKey goodKey = new GoodKey(customer);
		//	} else {

		//		JOptionPane.showMessageDialog(null, "Bad license key entered");
	//		}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

}// end of class

